<template>
  <div class="app-container">
    <el-tabs v-model="activeTab" v-loading="loading" type="border-card">
      <el-tab-pane label="秘钥配置" name="im">
        <el-form ref="form" :model="form" label-width="200px">
          <el-form-item label="商户ID">
            <el-input v-model="form.im_accessId" />
          </el-form-item>
          <el-form-item label="商户秘钥">
            <el-input v-model="form.im_secretkey" />
          </el-form-item>
          <el-form-item label="APPID">
            <el-input v-model="form.im_id" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">保 存</el-button>
            <el-button @click="onBack">返 回</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="基础配置" name="imBase">
        <el-form ref="form" :model="form" label-width="200px">
          <el-form-item label="APP名称">
            <el-input v-model="form.im_app_name" />
          </el-form-item>
          <el-form-item label="菜单名称">
            <el-input v-model="form.im_menu_name" />
          </el-form-item>
          <el-form-item label="菜单图标">
            <el-upload
              ref="iconAvatar"
              class="avatar-uploader"
              :action="upAction"
              :headers="upHeaders"
              :limit="1"
              :show-file-list="false"
              :on-success="handleSuccess"
              :before-upload="beforeUpload"
            >
              <img v-if="im_menu_icon_url" :src="im_menu_icon_url" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="菜单图标（选中后）">
            <el-upload
              ref="iconIngAvatar"
              class="avatar-uploader"
              :action="upAction"
              :headers="upHeaders"
              :limit="1"
              :show-file-list="false"
              :on-success="handleIngSuccess"
              :before-upload="beforeUpload"
            >
              <img v-if="im_menu_icon_ing_url" :src="im_menu_icon_ing_url" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="菜单链接">
            <el-input v-model="form.im_menu_url" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">保 存</el-button>
            <el-button @click="onBack">返 回</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { saveData, getData } from '../../api/config'
import { deepClone } from '../../utils'
import { getToken } from '../../utils/auth'
export default {
  data() {
    return {
      activeTab: 'im',
      loading: false,
      im_menu_icon_url: '',
      im_menu_icon_ing_url: '',
      upAction: process.env.VUE_APP_BASE_API + '/upload',
      upHeaders: {
        Authorization: getToken()
      },
      form: {
        im_accessId: '',
        im_secretkey: '',
        im_id: '',
        im_app_name: '',
        im_menu_name: '',
        im_menu_icon: '',
        im_menu_icon_ing: '',
        im_menu_url: ''
      }
    }
  },
  created() {
    this.getConfig()
  },
  methods: {
    async getConfig() {
      if (this.$store.getters.username !== 'demo') {
        const res = await getData()
        this.form = deepClone(res.data)
        this.im_menu_icon_url = res.data.im_menu_icon
      }
    },
    async onSubmit() {
      if (this.loading) {
        return
      }
      this.loading = true
      const res = await saveData(this.form)
      this.loading = false
      this.$message({
        type: (res.code === 200) ? 'success' : 'error',
        message: res.msg
      })
    },
    onBack() {
      this.$router.go(-1)
    },
    handleSuccess(res, file) {
      if (res.code === 200) {
        this.form.im_menu_icon = res.data.url
        this.im_menu_icon_url = URL.createObjectURL(file.raw)
      } else {
        this.$message({
          type: 'error',
          message: res.msg
        })
      }
      this.$refs['iconAvatar'].clearFiles()
    },
    handleIngSuccess(res, file) {
      if (res.code === 200) {
        this.form.im_menu_icon_ing = res.data.url
        this.im_menu_icon_ing_url = URL.createObjectURL(file.raw)
      } else {
        this.$message({
          type: 'error',
          message: res.msg
        })
      }
      this.$refs['iconIngAvatar'].clearFiles()
    },
    beforeUpload(file) {
      return true
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
